﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace MobileWeather
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void menuItemRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            string lcUrl = "http://weather.yahooapis.com/forecastrss?p=" + textBoxZipCode.Text.TrimEnd();

            // *** Establish the request 
            HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);

            // *** Set properties
            loHttp.Timeout = 10000;     // 10 secs

            // *** Retrieve request info headers
            HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();

            Encoding enc = Encoding.GetEncoding(1252);  // Windows default Code Page

            StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);

            string lcHtml = loResponseStream.ReadToEnd();

            RefreshScreen(lcHtml);

            loWebResponse.Close();
            loResponseStream.Close();
        }

        private void RefreshScreen(string input)
        {
            string titleEx = @"<title>Conditions for(?<title>[^@]+)</title>";
            string conditionsEx = @"<yweather:condition  text=""(?<desc>[^@]+)""  code=""(?<code>[^@]+)""  temp=""(?<temp>[^@]+)""  date=""(?<junk>[^@]+)"" ";
            string detailsEx = @"<yweather:forecast day=""(?<day>[^@]+)"" date=""(?<date>[^@]+)"" low=""(?<low>[^@]+)"" high=""(?<high>[^@]+)"" text=""(?<text>[^@]+)"" code=""(?<code>[^@]+)""";
            string imageEx = @"<img src=""(?<image>[^@]+)""/><br />";

            string desc = string.Empty;
            string temp = string.Empty;

            string day = string.Empty;
            string date = string.Empty;
            string low = string.Empty;
            string high = string.Empty;
            string text = string.Empty;

            string image = string.Empty;

            string title = string.Empty;

            Regex regEx = new Regex(conditionsEx);

            Match m = regEx.Match(input);

            if(m.Success)
            {
                desc = m.Groups["desc"].Value;
                temp = m.Groups["temp"].Value;
            }

            Regex regEx2 = new Regex(detailsEx);

            Match m2 = regEx2.Match(input);

            if(m2.Success)
            {
                day = m2.Groups["day"].Value;
                date = m2.Groups["date"].Value;
                text = m2.Groups["text"].Value;
                high = m2.Groups["high"].Value;
                low = m2.Groups["low"].Value;
            }

            Regex regExImage = new Regex(imageEx);

            Match m3 = regExImage.Match(input);

            if(m3.Success)
            {
                image = m3.Groups["image"].Value;
            }

            Regex regExTitle = new Regex(titleEx);
            Match mTitle = regExTitle.Match(input);

            if (mTitle.Success)
            {
                title = mTitle.Groups["title"].Value;
                title = string.Format("Conditions for ", title);
            }

            labelTitle.Text = title;

            labelCurrentConditionsValue.Text = string.Format("{0}, {1} F", desc, temp);
            labelForecastValue1.Text = string.Format("{0} - {1}. High: {2} Low: {3}", date, text, high, low);

            webBrowser.DocumentText = string.Format(@"<img src=""{0}""/>", image);
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            RefreshData();
        }
    }
}